/* 
 * File:   main2.cpp
 * Author: Vicky.H
 * Email:  eclipser@163.com
 */
#include <zmq.h>
#include <zmq_utils.h>
#include <assert.h>
#include <thread>
#include <iostream>

/*
 * 
 */
int main(void) {
    
    int r;
    const char* addr = "inproc://a";
    
    auto ctx = zmq_ctx_new();
    
    auto server = zmq_socket(ctx, ZMQ_PAIR);
    r = zmq_bind(server, addr);
    assert(r == 0);
    
    auto client = zmq_socket(ctx, ZMQ_PAIR);
    zmq_connect(client, addr);
    
    std::thread t1(
        [&server]() {
            int rc = 0;
            char buf[256] = {0};
            for(;;) {
                rc = zmq_recv(server, buf, sizeof(buf), 0);
                if (rc > 0) {
                    std::cout << "recv:" << rc << "\t" << buf << std::endl;
                    memset(buf, 0, rc);
                }
            }
        }
    );
    
    t1.detach();
    
    std::thread t2(
        [&client]() {
            int sc;
            char buf[11] = {0};
            buf[10] = '\0';
            for(;;) {
                std::this_thread::sleep_for(std::chrono::seconds(2));
                sprintf(buf, "%d", time(0));
                sc = zmq_send(client, buf, 11, 0);
            }
        }
    );
    
    t2.join();

    return 0;
}
